package com.lin.fengxiu.core.hack;

import com.sun.xml.internal.ws.api.databinding.MappingInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

/**
 * @ClassName AutoPrefixUrlMapping
 * @Description
 * @Author wushaopei
 * @Date 2021/3/7 15:02
 * @Version 1.0
 */

public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {

    @Value("${fengxiu.api-package}")
    private String apiPackagePath;

    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
//        String packageName = this.getPrefix(handlerType);
        if(mappingInfo != null){
            String prefix = this.getPrefix(handlerType);
            return RequestMappingInfo.paths(prefix).build().combine(mappingInfo);
        }
        return mappingInfo;
    }

    private String getPrefix(Class<?> handlerType){
        String packagName = handlerType.getPackage().getName();
        String dotPath = packagName.replaceAll(this.apiPackagePath,"");
        return dotPath.replace(".","/");
    }
}
